package com.example.bausip;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class OfficeHoursActivity extends Activity {

	ArrayList<NameEntity> questions;
	ArrayList<Item> questionItems;
	MyCustomAdapter dataAdapter;
	int instructor_id;
	String type,condition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_majors);
		Bundle extras = getIntent().getExtras();
		if(extras!=null)
		{
			instructor_id = extras.getInt("instructor_id");
			type = extras.getString("type");
		}
		getquestions();
	}

	public void getquestions() {
		Log.d("ray", "getting: " );
		String serverURL = new myURL("read_office_hours.php?i_id="+instructor_id,true).getURL();
		RZHelper p = new RZHelper(serverURL, this, "setQuestions", true);
		p.get();
		//setQuestions("","");
	}

	public void setQuestions(String s, String error) {
		Log.d("ray", "questionssssssssss: " + s);
		NameEntity ne = null;
		questions = new ArrayList<NameEntity>();
		ArrayList<OpenHours> myCs = new APIManager().getOfficeHours(s);
		for(int i =0;i<myCs.size();i++)
		{
			Log.d("ray"," Course id" +myCs.get(i).getId() + " ,name: "+myCs.get(i).toString());
			ne = new NameEntity(myCs.get(i).getId(), myCs.get(i).getOfficeDay(), "question");
			ne.setDescription(myCs.get(i).getOfficeFrom() + " -> "+ myCs.get(i).getOfficeTo());
			questions.add(ne);
		}
		questionItems = new ArrayList<Item>();
		ListView listView = (ListView) findViewById(R.id.list);

		listView.setTextFilterEnabled(true);
		Item question;
		if (questions.size() == 0) {
			questionItems.add(new Item(0, "", getString(R.string.empty_list)));
		} else {
			for (int i = 0; i < questions.size(); i++) {
				question = new Item(questions.get(i).getId(), "",
						questions.get(i).toString());
				question.setCode(questions.get(i).getDescription());
				questionItems.add(question); 
			}
			registerForContextMenu(listView);
			dataAdapter = new MyCustomAdapter(this, R.layout.name_list,
					questionItems);
			SharedMenu.adapter = dataAdapter;
			listView.setAdapter(dataAdapter);
			
		}
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.instructors , menu);
		SharedMenu.onCreateOptionsMenu(this, menu, getApplicationContext(),
				dataAdapter);
		return true;

	}
	public boolean onOptionsItemSelected(MenuItem item) {

		if (SharedMenu.onOptionsItemSelected(item, this) == false) {
			// handle local menu items here or leave blank

			switch (item.getItemId()) {
			case R.id.add:
				Intent intent = new Intent(this, CourseActivity.class);
				startActivity(intent);
				break;
			}
		}
		return super.onOptionsItemSelected(item);
	}

}
